package com.ruoyi.student.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.student.domain.StudentBaseInfo;
import com.ruoyi.student.mapper.StudentBaseInfoMapper;
import com.ruoyi.student.service.IStudentBaseInfoService;
import com.ruoyi.system.domain.SysPlace;
import com.ruoyi.system.mapper.SysPlaceMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 学生基础信息Service业务层处理
 *
 * @author linyang
 * @date 2024-01-31
 */
@Service
public class StudentBaseInfoServiceImpl extends ServiceImpl<StudentBaseInfoMapper, StudentBaseInfo> implements IStudentBaseInfoService {


    @Resource
    private StudentBaseInfoMapper studentBaseInfoMapper;

    @Resource
    private SysPlaceMapper sysPlaceMapper;


    /**
     * 查询学生基础信息
     *
     * @param id 学生基础信息主键
     * @return 学生基础信息
     */
    @Override
    public StudentBaseInfo selectStudentBaseInfoById(Long id) {
        return studentBaseInfoMapper.selectStudentBaseInfoById(id);
    }


    /**
     * 查询学生基础信息
     *
     * @param code 学号
     * @return
     */
    public StudentBaseInfo selectStudentBaseInfoByCode(String code) {
        return studentBaseInfoMapper.selectStudentBaseInfoByCode(code);
    }

    /**
     * 查询学生基础信息列表
     *
     * @param studentBaseInfo 学生基础信息
     * @return 学生基础信息
     */
    @Override
    public List<StudentBaseInfo> selectStudentBaseInfoList(StudentBaseInfo studentBaseInfo) {
        return studentBaseInfoMapper.selectStudentBaseInfoList(studentBaseInfo);
    }

    /**
     * 新增学生基础信息
     *
     * @param studentBaseInfo 学生基础信息
     * @return 结果
     */
    @Override
    public int insertStudentBaseInfo(StudentBaseInfo studentBaseInfo) {
        studentBaseInfo.setCreateTime(DateUtils.getNowDate());
        return studentBaseInfoMapper.insertStudentBaseInfo(studentBaseInfo);
    }

    /**
     * 修改学生基础信息
     *
     * @param studentBaseInfo 学生基础信息
     * @return 结果
     */
    @Override
    public int updateStudentBaseInfo(StudentBaseInfo studentBaseInfo) {
        studentBaseInfo.setUpdateTime(DateUtils.getNowDate());
        return studentBaseInfoMapper.updateStudentBaseInfo(studentBaseInfo);
    }

    /**
     * 批量删除学生基础信息
     *
     * @param ids 需要删除的学生基础信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentBaseInfoByIds(Long[] ids) {
        return studentBaseInfoMapper.deleteStudentBaseInfoByIds(ids);
    }

    /**
     * 删除学生基础信息信息
     *
     * @param id 学生基础信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentBaseInfoById(Long id) {
        return studentBaseInfoMapper.deleteStudentBaseInfoById(id);
    }


    /**
     * 修改学生生源地
     *
     * @param code
     * @param place
     * @return
     */
    @Override
    public int editStudentSource(String code, String place) {
        String[] arry = place.split(",");
        LambdaQueryWrapper<SysPlace> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlace::getProvince, arry[0]);
        if (arry.length == 2) {
            lambdaQueryWrapper.eq(SysPlace::getCity, arry[1]);
        }
        if (arry.length == 3) {
            lambdaQueryWrapper.eq(SysPlace::getCity, arry[1]);
            lambdaQueryWrapper.eq(SysPlace::getCounty, arry[2]);
        }
        SysPlace studentSourcePlace = sysPlaceMapper.selectOne(lambdaQueryWrapper);
        if (studentSourcePlace != null) {
            LambdaUpdateWrapper<StudentBaseInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(StudentBaseInfo::getSource, studentSourcePlace.getName());
            lambdaUpdateWrapper.set(StudentBaseInfo::getSourceFullName, studentSourcePlace.getFullName());
            lambdaUpdateWrapper.eq(StudentBaseInfo::getCode, code);
            return studentBaseInfoMapper.update(null, lambdaUpdateWrapper);
        } else {
            throw new ServiceException("数据库中未找到对应生源地！");
        }
    }


    /**
     * 根据学号判断学生是否存在
     *
     * @param code 学号
     * @return
     */
    public Boolean checkExistByCode(String code) {
        LambdaQueryWrapper<StudentBaseInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StudentBaseInfo::getCode, code);
        return studentBaseInfoMapper.exists(lambdaQueryWrapper);
    }


    /**
     * 定时任务更新学生信息
     *
     * @param studentBaseInfo
     * @return
     */
    public int taskUpdateStudentInfo(StudentBaseInfo studentBaseInfo) {
        LambdaUpdateWrapper<StudentBaseInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(StudentBaseInfo::getCode, studentBaseInfo.getCode());
        lambdaUpdateWrapper.set(StudentBaseInfo::getStudyState, studentBaseInfo.getStudyState());
        return studentBaseInfoMapper.update(null, lambdaUpdateWrapper);
    }


    /**
     * 根据辅导员工号查询学生数量
     *
     * @param counsellorCode
     * @return
     */
    public Long selectStudentCountByCounsellorCode(String year, String counsellorCode) {
        LambdaUpdateWrapper<StudentBaseInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(StudentBaseInfo::getCounsellorCode, counsellorCode);//辅导员工号
        lambdaUpdateWrapper.eq(StudentBaseInfo::getGraduationYear, year);//毕业年份
        return studentBaseInfoMapper.selectCount(lambdaUpdateWrapper);
    }
}
